﻿using System;
using System.Collections.Generic;
using NHibernate.Mapping.ByCode;
using Xenta.Enums;

namespace Xenta.Entities
{
    /// <summary>
    /// Represents the sales rule history entity.
    /// </summary>
    /// <remarks><pre>
    /// The entity regarding the disclosure level.
    /// 
    /// + Entire
    ///  + Extended
    ///   + Base
    ///    | - Rule(of the Base level)
    ///    | - Order(of the Keys level)
    ///    | - Item(of the Keys level)
    ///    | - Flags
    ///    | - UpdatedOn
    ///    | - CreatedOn
    ///    + Keys
    ///     + Primary
    ///      | - EntityID
    /// 
    /// Note: upper level includes lower level.
    /// </pre></remarks>
    public class SalesRuleHistoryEntity : AuditableEntity
    {
        #region Fields

        private OrderItemEntity _item;

        #endregion

        #region Ctors

        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SalesRuleHistoryEntity()
        {
            _item = null;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the rule.
        /// </summary>
        public virtual SalesRuleEntity Rule
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order.
        /// </summary>
        public virtual OrderEntity Order
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order item identifier.
        /// </summary>
        public virtual int ItemID
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the order item.
        /// </summary>
        public virtual OrderItemEntity Item
        {
            get
            {
                return ItemID == 0 ? null : _item;
            }
            set
            {
                _item = value;
                ItemID = value == null ? 0 : value.EntityID;
            }
        }

        /// <summary>
        /// Gets or sets the flags.
        /// </summary>
        public virtual SalesRuleHistoryFlags Flags
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override void Disclose(IDictionary<String, Object> obj,
            DisclosureLevel level,
            IDictionary<String, String> options)
        {
            base.Disclose(obj, level, options);
            if(level >= DisclosureLevel.Base)
            {
                obj["Rule"] = Rule.Disclose(DisclosureLevel.Base, options);
                obj["Order"] = Order.Disclose(DisclosureLevel.Keys, options);
                obj["Item"] = Item != null
                    ? Item.Disclose(DisclosureLevel.Keys, options)
                    : null;
                obj["Flags"] = Flags.ToString();
            }
        }

        #endregion
    }

    /// <summary>
    /// The entity NHibernate mapping.
    /// </summary>
    public class SalesRuleHistoryMapping : EntityMapping<SalesRuleHistoryEntity>
    {
        /// <summary>
        /// Initializes a new class instance.
        /// </summary>
        public SalesRuleHistoryMapping()
        {
            Table("fwk_SalesRuleHistory");
            Id(x => x.EntityID, m =>
            {
                m.Column("RecordID");
                m.Generator(Generators.Identity);
            });
            ManyToOne(x => x.Rule, m =>
            {
                m.Column("RuleID");
                m.ForeignKey("FK_fwk_SalesRuleHistory_fwk_SalesRules");
                m.NotNullable(true);
                m.Fetch(FetchKind.Join);
            });
            ManyToOne(x => x.Order, m =>
            {
                m.Column("OrderID");
                m.ForeignKey("FK_fwk_SalesRuleHistory_fwk_Orders");
                m.NotNullable(true);
            });
            Property(x => x.ItemID, m =>
            {
                m.Column("ItemID");
                m.NotNullable(true);
            });
            ManyToOne(x => x.Item, m =>
            {
                m.Column("ItemID");
                m.NotFound(NotFoundMode.Ignore);
                m.Insert(false);
                m.Update(false);
            });
            Property(x => x.Flags, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.CreatedOn, m =>
            {
                m.NotNullable(true);
            });
            Property(x => x.UpdatedOn, m =>
            {
                m.NotNullable(true);
            });
        }
    }
}
